define(['jquery',"bbcare","comm/datatable/datatable","bld/app/patient/patientList.servers","bld/app/patient/patientList.ui"],function($,$bb,$datatable,$servers,$ui){
    var patientList = {};
    var className = "applyTreatSchemeModal";

    /**
     * 给患者应用某个治疗方案成功后的回调函数
     */
    var ajaxApplyTreatSchemeCallBack = function(data){
        $bb.toastShow("应用方案成功！");
        $("." + className).modal("hide");
        $datatable.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
    }

    /**
     * 给患者应用某个治疗方案
     */
    var onSubmit = function(){
        var info = $(this).data("info");
        var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
        var channelId = $bb.getChannelId();
        var schemeId = $(".apply-scheme-box select").val();
        var schemeName = $(".apply-scheme-box select option:selected").text();
        var params ={
            "oprInfo":{
                "opCode":"1242",
                "opType":"2",
                "opNote":"【"+doctorName+"】给【"+info.userName+"】患者应用【"+ schemeName +"】治疗方案",
                "channelType":"2",
                "channelId":channelId
            },
            "planInfo":{
                "userId":info.userId,
                "queueId":info.queueId,
                "schemeId":schemeId,
                "groupType":"0",
                "groupValue":info.groupValue,
                "clUserId":info.clUserId,
                "teUserId":info.teUserId,
            }
        }
        $servers.ajaxApplyTreatScheme(params,ajaxApplyTreatSchemeCallBack);
    }

    /**
     * 选择一个患者
     */
    var onUserSelect = function(){
        var userList = $(this).data("userList");
        var index = $(this).index();
        $("#userList").css({"display":"none"});
        $ui.info({
            "id":"applySchemeInfo",
            "data":userList[index]
        });
        $("#applySchemeSubmit").unbind("click");
        $("#applySchemeSubmit").data("info",userList[index]);
        $("#applySchemeSubmit").click(onSubmit);
    }

    var ajaxCreatAuthorCallBack = function (data) {
        if( data.rows.length > 0 ){
            $ui.creatAuthor({
                "id":"createAuthor",
                "list":data.rows,
            });
        }
    }

    /**
     * 查询建档患者成功后的回调函数
     */
    var ajaxArchivePatientCallBack = function(data,params){
        if( params.queryCriteria.fuzzyVar ){
            $ui.userList({
                "id":"userList",
                "list":data.rows
            });
            $("#userList").css({"display":"block"});
            $("#userList div.item").unbind("click");
            $("#userList div.item").data("userList",data.rows);
            $("#userList div.item").click(onUserSelect);
        }else{
            $("#userList").css({"display":"none"});
        }
    }

    /**
     * 查询建档患者
     */
    var onSearchUser = function(){
        var fuzzyVar = $(this).val();
        var params = {
            "queryCriteria":{
                "fuzzyVar":fuzzyVar,
                //"isApplyFalse":1,
            }
        }
        $servers.ajaxArchivePatient(params,ajaxArchivePatientCallBack);
    }

    /**
     * 清空搜索框
     */
    var onEmptySearch = function(){
        $("#searchUser").val("");
        $("#userList").css({"display":"none"});
    }

    /**
     * 查询方案信息成功后的回调函数
     */
    var ajaxAllSchemeCallBack = function(data){
        $bb.modal({
            "className":className,
            "tit":"应用方案",
        },function(){
            $ui.applyTreatScheme({
                "dom":$("." + className + " .modal-body"),
                "schemes":data.rows
            });
            $("." + className).modal("show");
            $("#searchUser").unbind("input propertychange");
            $("#searchUser").bind("input propertychange",onSearchUser);
            $("#emptyBtn").unbind("click");
            $("#emptyBtn").click(onEmptySearch);
        });
    }

    /**
     * 点击“应用方案”
     */
    var onApplyTreatScheme = function(){
        var params = {
            "queryCriteria":{
                "schemeStatus":"1"
            }
        };
        $servers.ajaxAllScheme(params,ajaxAllSchemeCallBack);
    }

    patientList.init = function(){
        // 应用方案
        $("#applyTreatScheme").unbind("click");
        $("#applyTreatScheme").click(onApplyTreatScheme);
        // datatable
        var id = $(".layout-bottom table").attr("id");
        $datatable.init({
            "id": id,
            "options": $servers.getOptions()
        });
        // 获取录入者
        $servers.ajaxCreatAuthor(ajaxCreatAuthorCallBack)
    };

    return patientList;
});